package com.qidian.mes.pro.controller;

import com.alibaba.excel.EasyExcel;
import com.qidian.mes.pojo.Order;
import com.qidian.mes.pro.service.OrderService;
import com.qidian.mes.util.Tools;
import com.qidian.mes.util.dot.PagerDTO;
import com.qidian.mes.util.pojo.QdResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author dqy
 * @Description
 * @Time 2024/4/25 16:13
 */
@RestController
@RequestMapping("/order")
@CrossOrigin
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 保存工单
     */
    @PostMapping("/saveOrder")
    public QdResult saveOrder(@RequestBody Order order) {
        System.out.println(order);
        return orderService.saveOrder(order);
    }
    /**
     * 更新工单
     */
    @PostMapping("/updateOrder")
    public QdResult updateOrder(@RequestBody Order order) {
        System.out.println("拿到的order是"+order);
        return orderService.updateOrder(order);
    }
    /**
     * 删除工单
     */
    @GetMapping("/delete")
    public QdResult deleteOrder(Long woId) {
        return orderService.deleteOrder(woId);
    }
    /**
     * 批量删除工单
     */
    @GetMapping("/deletes")
    public QdResult deletes(Long [] woIds) {
        return orderService.deletes(woIds);
    }
    /**
     * 分页查询工单
     */
    @GetMapping("/queryByPage")
    public QdResult<PagerDTO<Order>> queryByPage(Order order, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit) {
        return orderService.queryByPage(order, page, limit);
    }
    /**
     * 根据id查询工单
     */
    @GetMapping("/queryById")
    public QdResult<Order> queryById(Long woId) {
        return orderService.queryById(woId);
    }
    /**
     * 查询所有工单
     */
    @GetMapping("/queryAll")
    public QdResult queryAll() {
        return orderService.queryAll();
    }
    /**
     * 导出生产工单
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        QdResult<List<Order>> qdResult = orderService.queryAll();
        List<Order> orders = qdResult.getData();
        List<Order> tempOrders = new ArrayList<Order>();
        for (Order order : orders) {
            if (order.getChildren()!=null){
                for (Order child : order.getChildren()) {
                    if (child!=null){
                        tempOrders.add(child);
                    }
                }
            }
        }
        for (Order tempOrder : tempOrders) {
            orders.add(tempOrder);
        }
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("生产工单-" + System.currentTimeMillis(), "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), Order.class).sheet("生产工单")
                    .doWrite(orders);
        } catch (UnsupportedEncodingException e) {
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().println(Tools.object2json(QdResult.error("下载文件失败")));
        }
    }
}
